<template>
	<view>
		
	</view>
</template>

<script>
	import config from '@/config/config'
	import { wxOauth2 } from '@/api/login.js'
	import { mapActions } from 'vuex'
	export default {
		data() {
			return {}
		},
		onLoad(query) {
			let state = query.state
			let code = query.code
			
			if(code && state == config.state) {
				uni.showLoading({
					title: "正在登录中",
					mask: true
				})
				this.getAccessToken(code)
			}
			
		},
		watch: {},
		methods: {
			getAccessToken(code) {
				wxOauth2(code).then((res) => {
					uni.hideLoading()
					if(res.code == 500) {
						return uni.showModal({
							"title": "登录出错",
							showCancel: false,
							complete: () => {
								uni.switchTab({
									url: "/pages/index/index"
								})
							}
						})
					}
					const token = res.msg
					this.$store.dispatch('setToken', token);
					uni.switchTab({
						url: "/pages/index/index"
					})
				}).catch(err => {
					uni.hideLoading()
					uni.showModal({
						"title": "登录出错",
						showCancel: false,
						complete: () => {
							uni.switchTab({
								url: "/pages/index/index"
							})
						}
					})
				})
			},
		}
	}
</script>

<style>
</style>